package com.syn.vr.util;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.core.content.ContextCompat;

import com.syn.vr.R;

public class MyTextUtil {

    /**
     * 修改start到end的文字颜色
     */
    public static SpannableString setForegroundColor(Context context, int color, String text, int start, int end, int flags) {
        ForegroundColorSpan span = new ForegroundColorSpan(ContextCompat.getColor(context, color));
        SpannableString ss = new SpannableString(text);
        ss.setSpan(span, start, end, flags);
        return ss;
    }

    /**
     * 文字添加红色星号
     */
    public static Spannable setStar(Context context, String text) {
        return setForegroundColor(context, R.color.red, "*" + text, 0, 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    }

    public static Spannable setStar(Context context, int text) {
        return setStar(context, context.getString(text));
    }

    public static String getEtText(AppCompatEditText editText) {
        return editText.getText() == null ? "" : editText.getText().toString();
    }

    public static long toDec(byte[] bytes) {
        long result = 0;
        long factor = 1;
        for (int i = 0; i < bytes.length; ++i) {
            long value = bytes[i] & 0xffl;
            result += value * factor;
            factor *= 256l;
        }

        return result;
    }
}
